﻿using System;

namespace HelperLibrary
{
    /// <summary>
    /// 日期工具类
    /// </summary>
    public static class DateTimeHelper
    {
        /// <summary>
        /// yyyy-MM-dd
        /// </summary>
        /// <returns></returns>
        public static string ToYMD(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToYMDHMS(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToNum(this DateTime dateTime)
        {
            return dateTime.ToString("yyyyMMddHHmmss");
        }

        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToYMDHM(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm");
        }

        /// <summary>
        /// HH:mm
        /// </summary>
        /// <returns></returns>
        public static string ToHHmm(this DateTime dateTime)
        {
            return dateTime.ToString("HH:mm");
        }

        /// <summary>
        /// 时间转换为时间戳
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static long ToUnixTime(this DateTime date)
        {
            if (date == null || date == DateTime.MinValue)
            {
                return 0;
            }
            DateTime startTime = new DateTime(1970, 1, 1).ToLocalTime();
            long timeStamp = (long)(date - startTime).TotalSeconds; // 相差秒数
            return timeStamp;
        }

        /// <summary>
        /// 求两个日期间隔时间
        /// </summary>
        /// <param name="dateTime1"></param>
        /// <param name="dateTime2"></param>
        /// <returns></returns>
        public static double DateDiff(DateTime dateTime1, DateTime dateTime2, TimeType timeType = TimeType.Minute)
        {
            TimeSpan ts1 = new TimeSpan(dateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(dateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();

            double result = 0;
            switch (timeType)
            {
                case TimeType.Day:
                    result = ts.TotalDays;
                    break;
                case TimeType.Hour:
                    result = ts.TotalHours;
                    break;
                case TimeType.Minute:
                    result = ts.TotalMinutes;
                    break;
                case TimeType.Second:
                    result = ts.TotalSeconds;
                    break;
                default:
                    result = ts.TotalMinutes;
                    break;
            }

            return result;
        }

        /// <summary>
        /// 分钟转换为小时分钟字符串
        /// </summary>
        /// <param name="minutes"></param>
        /// <returns></returns>
        public static string MinutesToHourStr(int minutes)
        {
            return $"{minutes / 60}小时{minutes % 60}分钟";
        }

        /// <summary>
        /// 获取中文星期
        /// </summary>
        /// <param name="ENDayOfWeek"></param>
        /// <returns></returns>
        public static string GetCNDayOfWeek(string ENDayOfWeek)
        {
            switch (ENDayOfWeek)
            {
                case "Monday":
                    return "星期一";
                case "Tuesday":
                    return "星期二";
                case "Wednesday":
                    return "星期三";
                case "Thursday":
                    return "星期四";
                case "Friday":
                    return "星期五";
                case "Saturday":
                    return "星期六";
                case "Sunday":
                    return "星期天";
                default:
                    throw new Exception("Invalid ENDayOfWeek");
            }
        }

        /// <summary>
        /// 获取中文星期
        /// </summary>
        /// <param name="ENDayOfWeek"></param>
        /// <returns></returns>
        public static int GetNumOfWeek(string ENDayOfWeek)
        {
            switch (ENDayOfWeek)
            {
                case "Monday":
                    return 1;
                case "Tuesday":
                    return 2;
                case "Wednesday":
                    return 3;
                case "Thursday":
                    return 4;
                case "Friday":
                    return 5;
                case "Saturday":
                    return 6;
                case "Sunday":
                    return 7;
                default:
                    throw new Exception("Invalid ENDayOfWeek");
            }
        }

        /// <summary>
        /// 获取日期所在周的某一天的日期
        /// </summary>
        /// <param name="date"></param>
        /// <param name="weekDay"></param>
        /// <returns></returns>
        public static DateTime GetWeekDay(DateTime date, int weekDay)
        {
            if (date.DayOfWeek == 0)
            {
                date = date.AddDays(-1);
            }
            return date.AddDays((-(int)date.DayOfWeek) + weekDay);
        }

        /// <summary>
        /// 生成GUID
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static string CreateGUID(int type = 1)
        {
            var uuid = "";
            switch (type)
            {
                case 0:
                    uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12
                    break;
                case 1:
                    uuid = Guid.NewGuid().ToString("N"); // e0a953c3ee6040eaa9fae2b667060e09
                    break;
                case 2:
                    uuid = Guid.NewGuid().ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12
                    break;
                case 3:
                    uuid = Guid.NewGuid().ToString("B"); // {734fd453-a4f8-4c5d-9c98-3fe2d7079760}
                    break;
                case 4:
                    uuid = Guid.NewGuid().ToString("P"); // (ade24d16-db0f-40af-8794-1e08e2040df3)
                    break;
                case 5:
                    uuid = Guid.NewGuid().ToString("X"); // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}}
                    break;
            }
            return uuid;
        }
    }

    /// <summary>
    /// 时间类型枚举
    /// </summary>
    public enum TimeType
    {
        /// <summary>
        /// 天
        /// </summary>
        Day = 3,
        /// <summary>
        /// 小时
        /// </summary>
        Hour = 4,
        /// <summary>
        /// 分钟
        /// </summary>
        Minute = 5,
        /// <summary>
        /// 秒
        /// </summary>
        Second = 6
    }
}
